﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArgusPCLib.Threading.Tasks
{
	public delegate IEnumerable<BackgroundWorkerTask> TaskAction(ThreadState threadState, object asyncState);

	public class BackgroundWorkerTask
	{
		public TaskAction TaskAction { get; private set; }
		public object AsyncState { get; private set; }

		public BackgroundWorkerTask(TaskAction taskAction, object asyncState)
		{
			this.TaskAction = taskAction;
			this.AsyncState = asyncState;
		}

		public static implicit operator TaskAction(BackgroundWorkerTask task)
		{
			return task.TaskAction;
		}

		public static implicit operator BackgroundWorkerTask(TaskAction task)
		{
			return new BackgroundWorkerTask(task, null);
		}
	}

	public class StartTasksSettings
	{
		public int NumberOfThreads { get; private set; }
		public event TaskCompletedEventHandler TaskCompleted;
		public ThreadState ThreadState { get; private set; }

		public StartTasksSettings()
			: this(0, null) { }

		public StartTasksSettings(int numberOfThreads, ThreadState threadState)
		{
			if (numberOfThreads < 1)
				this.NumberOfThreads = System.Environment.ProcessorCount;
			else
				this.NumberOfThreads = numberOfThreads;

			if (threadState == null)
				this.ThreadState = new ThreadState();
			else
				this.ThreadState = threadState;
		}

		internal void RaiseTaskCompleted(object sender, TaskCompletedEventArgs e)
		{
			if (this.TaskCompleted != null)
				this.TaskCompleted(sender, e);
		}
	}

	public enum BackgroundWorkerStates
	{
		Stopped,
		Running,
		Idle
	}
}
